
package com.probiz.estoresa.customer.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.common.model.customer.Customer;
import com.probiz.estore.common.model.customer.Membership;
import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.customer.model.CustomerSearchCriteria;
import com.probiz.estore.customer.service.CustomerManager;
import com.probiz.estore.customer.service.MembershipManager;

public class CustomerSelectorController extends GenericStoreAdminController<Customer> {
	private CustomerManager		customerManager	= null;
	private MembershipManager   membershipManager = null;
	public void setCustomerManager(CustomerManager inMgr) {
		this.customerManager = inMgr;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#getEntityName(java.lang.Object)
	 */

	public void setMembershipManager(MembershipManager membershipManager) {
		this.membershipManager = membershipManager;
	}


	@Override
	protected String getEntityName(Customer entity) {
		return null;
	}




	@Override
	protected void initController() throws Exception {
		mgr = customerManager;
	}


	@Override
	protected void preSave(HttpServletRequest request, Customer entity,
			BindException errors) {
		
	}


	@SuppressWarnings("unchecked")
	public ModelAndView defaultAction(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("customer/customerSelector");
		List<Membership> membershipList = membershipManager.getAllMembershipsOrderByLevel(true);
		mv.addObject("membershipList", membershipList);
		return mv;
	}

	public ModelAndView search(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("customer/include/customerSelectorResults");
		
		SearchCriteria searchCriteria = createSearchCriteria(request);
		CustomerSearchCriteria customerSearchCriteria = new CustomerSearchCriteria();
		try {
			// 绑定参数
			bind(request, customerSearchCriteria);
			List<Customer> customerList = customerManager.searchByCriteriaForController(customerSearchCriteria,	searchCriteria);
			mv.addObject("customerList", customerList);
			mv.addObject("customerSearchCriteria", customerSearchCriteria);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return mv;
	}
}
